<?php
include_once dirname(__FILE__)."/common/basic.php";
include_once dirname(__FILE__)."/common/Tencent.php";

$func->validateLogin();
$uid = $_SESSION[uid];
$nav = $_GET[nav];
$user = $func->getUserById($uid);
if(!$nav){
	$nav="profile";
}
if($nav=='profile'){
	if($_POST[r]){
		$user_email = trim(strtolower($_POST[mail]));
		$user_name = trim($_POST[name]);
		$user_website = trim($_POST[website]);
		$user_mydesc = trim($_POST[mydesc]);
		
		$err = $func->profile_validate_field($user,$user_email, $user_name, $user_website, $user_mydesc);
		if(count($err)==0){
			$profile_update = $func->profile_update($user,$user_email,$user_name,$user_website,$user_mydesc);
		}
	}else{
		$user_email = $user[email];
		$user_name = $user[name];
		$user_website = $user[website];
		$user_mydesc = $user[mydesc];
	}
}elseif($nav=='pwd'){
	if($_POST[r]){
		$user_pwd = trim($_POST[pwd]);
		$user_newpwd = trim($_POST[newpwd]);
		$user_newpwd2 = trim($_POST[newpwd2]);
		if(!$user_pwd){
			$e_pwd = "密码不能为空";
		}else{
			$user_pwd_encoded = $func->password_encode($user_pwd);
			if($user[password]!=$user_pwd_encoded){
				$e_pwd = "当前密码不正确";
			}
		}
		if(!$user_newpwd){
			$e_newpwd = "密码不能为空";
		}else{
			$res = $func->v_password($user_newpwd);
			if($res!==true){
				$e_newpwd = $res;
			}
		}
		if(!$user_newpwd2){
			$e_newpwd2 = "密码不能为空";
		}else{
			if($user_newpwd!=$user_newpwd2){
				$e_newpwd2 = "两次输入密码不一致";
			}
		}
		if(!$e_pwd && !$e_newpwd && !$e_newpwd2){
			$pwd_update = $func->user_set_password($user[uid],$user_newpwd);
		}
	}
}elseif($nav=='avatar'){
	if($_POST[r]){
		if(!$_FILES[avatarfile][name]){
			$err = "没有选择图片文件";
		}else{
			$file = array(
				'name'=>$_FILES[avatarfile][name],
				'type'=>$_FILES[avatarfile][type],
				'tmp_name'=>$_FILES[avatarfile][tmp_name],
				'size'=>$_FILES[avatarfile][size],
				'error'=>$_FILES[avatarfile][error]
			);
			$size = getimagesize($file[tmp_name]);
			if(!$size){
				$err = "上传的不是图片文件";
			}else{
				$width = $size[0];
				$height = $size[1];
				if($width<100 || $height<100){
					$err = "图片像素太低";
				}elseif($file[size]/1024>1024*8){
					$err = "图片大小超过8MB";
				}else{
					$fid_img = $func->id_create();
					$path_img = $func->buildAvatarPath($fid_img);
					move_uploaded_file($file[tmp_name], $path_img);
					
					$fid_img_dest = $func->id_create();
					$path_img_dest = $func->buildAvatarPath($fid_img_dest);
					$res = $func->image_resize($path_img, 200, 200, $path_img_dest,$width>$height?"w":"h");
					
					$fid_img_dest_m = $func->id_create();
					$path_img_dest_m = $func->buildAvatarPath($fid_img_dest_m);
					$res_m = $func->image_resize($path_img, 50, 50, $path_img_dest_m,$width>$height?"w":"h");
					
					$fid_img_dest_s = $func->id_create();
					$path_img_dest_s = $func->buildAvatarPath($fid_img_dest_s);
					$res_s = $func->image_resize($path_img, 30, 30, $path_img_dest_s,$width>$height?"w":"h");
					
					if($res && $res_m && $res_s){
						$old_avatar_fid = $user[avatar_fid];
						$old_avatar_fid_m = $user[avatar_fid_m];
						$old_avatar_fid_s = $user[avatar_fid_s];
						
						$file[tmp_name] = $path_img_dest;
						$file[size] = filesize($file[tmp_name]);
						$avatar_update = $func->avatar_update($user[uid],$fid_img_dest,$fid_img_dest_m,$fid_img_dest_s);
						if($avatar_update){
							//delete old avatar files
							if($old_avatar_fid && file_exists($func->buildAvatarPath($old_avatar_fid))){
								unlink($func->buildAvatarPath($old_avatar_fid));
							}
							if($old_avatar_fid_m && file_exists($func->buildAvatarPath($old_avatar_fid_m))){
								unlink($func->buildAvatarPath($old_avatar_fid_m));
							}
							if($old_avatar_fid_s && file_exists($func->buildAvatarPath($old_avatar_fid_s))){
								unlink($func->buildAvatarPath($old_avatar_fid_s));
							}
							$user[avatar_fid] = $fid_img_dest;
						}
					}else{
						$err = "转换图片时出错";
					}
				}
			}
		}
	}
}elseif($nav=='oauth'){
	if($_GET[connect]=="tencent"){//同步腾讯微博
		$tencent_callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']."?nav=oauth&connect=tencent";
		if ($_GET['code']) {//第二步：用得到的code请求accesstoken。
	        $code = $_GET['code'];
	        $openid = $_GET['openid'];
	        $openkey = $_GET['openkey'];
	        //获取授权token
	        $url = OAuth::getAccessToken($code, $tencent_callback);
	        $r = TencentHttp::request($url);
	        parse_str($r, $out);
	        //存储授权数据
	        if ($out['access_token']) {
	        	$out[code]=$_GET[code];
	        	$out[openkey]=$_GET[openkey];
	        	$out[openid] = $_GET[openid];
	            $func->oauth_tencent_info_save($uid,$out);
	            $func->oauth_tencent_info_makesession($uid);
	            //验证授权
	            $r = OAuth::checkOAuthValid();
	            if(!$r){
	                $err = "授权失败，请稍候重试。。。";
	            }
	        } else {
	        	$err = "获取口令失败，请稍候重试。。。";
	        }
	    }else{//第一步：请求code
			$tencent_authorized_url = OAuth::getAuthorizeURL($tencent_callback);
	    	header('Location: ' . $tencent_authorized_url);
	    	die();
	    }
	}elseif($_GET[disconnect]=="tencent"){
		$func->oauth_tencent_info_delete($uid);
	}
	$oauth_tencent = $func->oauth_tencent_info_get($uid);
}
$tl->set_file("account");
$tl->p();
?>